package com.hl.card.modular.pack.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.persistence.dao.CommunityUserMapper;
import com.hl.card.common.persistence.model.*;
import com.hl.card.common.remote.resolve.RemoteService;
import com.hl.card.common.remote.user.RemoteUser;
import com.hl.card.common.remote.user.RemoteUserService;
import com.hl.card.common.util.*;
import com.hl.card.core.shiro.ShiroKit;
import com.hl.card.modular.pack.service.*;
import com.hl.card.modular.packBack.service.ISalesDivisionService;
import com.hl.card.modular.system.service.ISysDicService;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户个人资料控制器
 *
 * @author zyb
 */
@RestController
@RequestMapping("/api/user")
public class UserDataController {

    private static final Logger logger = LoggerFactory.getLogger(UserDataController.class);
    @Autowired
    CommunityUserMapper communityUserMapper;
    @Autowired
    ISysDicService sysDicService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private UserDataService userDataService;
    @Autowired
    private PackUserService packUserService;
    @Autowired
    private CollectAddressService collectAddressService;
    @Autowired
    private DisMemberInfoOenService disMemberInfoOenService;
    @Autowired
    ISalesDivisionService iSalesDivisionService;
    @Autowired
    private ResponseData responseData;
    @Autowired
    private RemoteService remoteService;
    @Autowired
    private ZoneUserAwardService zoneUserAwardService;

    @Autowired
    private ZoneUserAwardThreeService zoneUserAwardThreeService;

    @Value("${uc.center}")
    private String userCenterUrl;
    private static String picUrl = "https://hlbibi.oss-cn-shenzhen.aliyuncs.com/usercenter/headimg/";

    /**
     * 个人资料
     *
     * @param user
     * @return
     */
    @GetMapping(value = "/queryUserdata")
    public ResponseObject queryUserdata(User user) {
        String salt = ShiroKit.getRandomSalt(5);
        String iscommunityUser = "0";
        ResponseObject res = new ResponseObject();
        User newUser = new User();
        Wrapper<User> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("user_id", user.getUserId());
        newUser = userDataService.selectOne(wrapper1);
        Map map = new HashMap<>();
        RemoteUser remoteUser = null;
        JSONObject json = remoteUserService.findSsoUserByUserId(user.getUserId());
        if (json.get("code").equals("0")) {
            JSONObject userObj = (JSONObject) json.get("user");
            if (userObj != null) {
                remoteUser = JSON.parseObject(userObj.toJSONString(), RemoteUser.class);
            }
        }
        //对无卡包用户的情况进行补充初始化
        if (newUser == null && remoteUser != null) {
            RemoteUser newRemoteUser = new RemoteUser();
            newRemoteUser.setLoginAccount(remoteUser.getMobilePhone());
            newRemoteUser.setMobilePhone(remoteUser.getMobilePhone());
            String userid = remoteUser.getUserId();
            newRemoteUser.setUserId(userid);
            newUser = new User();
            newUser = userDataService.ssouserToCarPackUser(newRemoteUser, newUser);
            newUser.setSalt(salt);
            userDataService.insert(newUser);
        }
        if (remoteUser != null) {
            //对头像进行特别处理
            String remoteImg = remoteUser.getImg();
            if (!StringUtils.isBlank(remoteImg)) {
                newUser.setImg(remoteUser.getImg());
            }
            if (remoteUser.getSex() != null) {
                newUser.setSex(Integer.parseInt(remoteUser.getSex()));
            } else {
                newUser.setSex(3);
            }
            newUser.setName(remoteUser.getUserName());
            newUser.setPhone(remoteUser.getMobilePhone());
            newUser.setEmail(remoteUser.getEmail());
            User u = userDataService.selectOne(wrapper1);
            if (u != null) {
                if (u.getRefer_reg_code() != null) {
                    User p_user = userDataService.findUserByRedCode(u.getRefer_reg_code());
                    if (p_user != null) {
                        map.put("parent_phone", p_user.getPhone());
                    }

                }
                newUser.setReg_code(u.getReg_code());
                newUser.setRefer_reg_code(u.getRefer_reg_code());
                newUser.setCreatetime(u.getCreatetime());
                newUser.setIscommunityUser(u.getIscommunityUser());
            }
        } else {
            newUser = userDataService.selectOne(wrapper1);
            if (newUser!=null&&newUser.getRefer_reg_code() != null) {
                User p_user = userDataService.findUserByRedCode(newUser.getRefer_reg_code());
                if (p_user != null) {
                    map.put("parent_phone", p_user.getPhone());
                }
            }
        }
        Wrapper<DisMemberInfoOen> wrapper = new EntityWrapper<>();
        wrapper.eq("dis_user_id", user.getUserId());
        DisMemberInfoOen disMemberInfo = disMemberInfoOenService.selectOne(wrapper);
        map.put("user", newUser);
        map.put("memberInfo", disMemberInfo);
        if (null == newUser) {
            res.setErroCode(1);
            res.setData("没有此用户");
        } else {
            res.setErroCode(0);
            res.setData("此用户存在");
        }
        map.put("community",newUser!=null?newUser.getIscommunityUser():"0");  //合伙人
        map.put("ismember", 1);  //合伙人
        res.setData(map);
        res.setErroCode(0);
        return res;
    }

    /**
     * 发送post请求到卡包后台
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            //新添加的请求头编码
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 修改会员性别
     *
     * @param userId
     * @param sex
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/modifyUserSex")
    public ResponseObject modifyUserSex(String userId, String sex) throws IOException {
        ResponseObject res = new ResponseObject();
        userDataService.updateUserSex(userId, sex);
        if (null == userId || null == sex) {
            res.setErroCode(1);
            res.setData("修改姓名失败");
        } else {
            res.setErroCode(0);
            res.setData("修改性别成功");
            System.out.println("用户ID" + userId + "用户性别" + sex);
        }
        return res;
    }

    /**
     * 修改头像路劲
     *
     * @param userId img
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/modifyUserImg")
    public ResponseObject modifyUserImg(String userId, @RequestParam("img") MultipartFile img) throws IOException {
        //todo 是指戒返回路径还是返回去base64
        //获取文件后缀
        String suffix = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
        String fileName = userId.substring(0, 5) + System.currentTimeMillis() + suffix;
        if (img.getBytes().length > 1 * 1024 * 1024) {//查过2M 压缩到0.5M
            byte[] bt = OssTool.compressPicForScale(img.getBytes(), 512l);
            OssTool.upload(bt, fileName);
        } else {
            OssTool.upload(img.getBytes(), fileName);
        }
        RemoteUser remoteUser = remoteService.findUserById(userId);
        String oldImg = remoteUser.getImg();
        //上传成功，删除旧头像
        if (!StringUtils.isBlank(oldImg) && oldImg.length() < 2000) {
            OssTool.delete(oldImg);
        }
        //将base64 存入
        ResponseObject res = new ResponseObject();
        userDataService.updateUserImg(userId, picUrl + fileName);
        if (null == userId || null == img) {
            res.setErroCode(1);
            res.setData("修改头像失败");
        } else {
            res.setErroCode(0);
            JSONObject object = new JSONObject();
            object.put("msg", "修改头像成功");
            object.put("img", fileName);
            res.setData(object);
            System.out.println("" + userId + "用户头像" + img);
        }
        return res;
    }

    /**
     * 修改头像路劲
     *
     * @param userId img
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/modifyUserName")
    public ResponseObject modifyUserName(String userId, String userName) throws IOException {
        ResponseObject res = new ResponseObject();
        userDataService.updateUserName(userId, userName);
        if (null == userId || null == userName) {
            res.setErroCode(1);
            res.setData("修改名称失败");
        } else {
            res.setErroCode(0);
            res.setData("修改名称成功");
            System.out.println("" + userId + "用户名称" + userName);
        }
        return res;
    }

    /**
     * 修改绑定邮箱地址
     */
    @PostMapping("/modifyEmail")
    public Object modifyEmail(String userId, String addr, String phone, String code) throws IOException {
        ResponseObject res = new ResponseObject();
        Object object = userDataService.modifyEmail(userId, addr, phone, code);
        if (userId == null && userId == "") {
            res.setErroCode(1);
            res.setData("id不能为空");
        }
        if (phone == null && phone == "") {
            res.setErroCode(1);
            res.setData("手机号码不得为空");
        }
        if (code == null && code == "") {
            res.setErroCode(1);
            res.setData("验证码不得为空");
        }
        if (addr == null && addr == "") {
            res.setErroCode(1);
            res.setData("邮箱不得为空");
        } else {
            res.setErroCode(0);
            res.setData(object);
        }
        return res;
    }

    /**
     * 发送手机验证码
     *
     * @param
     * @return res
     * @throws IOException
     */
    @PostMapping(value = "/outSendPhoneCode")
    public ResponseObject outSendPhoneCode(String userId, String phoneNum) throws IOException {
        ResponseObject res = new ResponseObject();
        userDataService.outSendPhoneCode(userId, phoneNum);
        if (userId == null && userId == "") {
            res.setErroCode(1);
            res.setData("id不能为空");
        }
        if (phoneNum == null && phoneNum == "") {
            res.setErroCode(1);
            res.setData("手机验证码不得为空");
        } else {
            res.setErroCode(0);
            res.setData("发送验证码成功");
        }
        return res;
    }

    /**
     * 绑定用户手机
     *
     * @param
     * @return res
     * @throws IOException
     */
    @PostMapping(value = "/outChangePhone")
    public ResponseObject outChangePhone(String userId, String newPhone, String verificode) throws IOException {
        ResponseObject res = new ResponseObject();
        Object object = userDataService.outChangePhone(userId, newPhone, verificode);
        if (userId == null && userId == "") {
            res.setErroCode(1);
            res.setData("id不能为空");
        }
        if (newPhone == null && newPhone == "") {
            res.setErroCode(1);
            res.setData("手机号码不得为空");
        }
        if (verificode == null && verificode == "") {
            res.setErroCode(1);
            res.setData("验证码不得为空");
        } else {
            res.setErroCode(0);
            res.setData(object);
        }
        return res;
    }

    /**
     * 修改用户密码
     *
     * @param
     * @return res
     * @throws IOException
     */
    @PostMapping(value = "/modifyChangePwd")
    public ResponseObject modifyChangePwd(String userId, String sourcePwd, String loginPwd) throws IOException {
        ResponseObject res = new ResponseObject();
        Object object = userDataService.modifyChangePwd(userId, sourcePwd, loginPwd);
        if (userId == null && userId == "") {
            res.setErroCode(1);
            res.setData("id不能为空");
        }
        if (sourcePwd == null && sourcePwd == "") {
            res.setErroCode(1);
            res.setData("原密码不得为空");
        }
        if (loginPwd == null && loginPwd == "") {
            res.setErroCode(1);
            res.setData("新密码不得为空");
        } else {
            res.setErroCode(0);
            res.setData(object);
        }
        return res;
    }

    /**
     * 修改支付密码
     *
     * @param
     * @return
     */
    @GetMapping(value = "/modifyUserPayPassword")
    public ResponseData modifyUserPayPassword(String userId, String userPayPassword, String newPassword) {
        responseData.setData(null);
        ResponseObject res = new ResponseObject();
        /* DisMemberInfo dmi= new DisMemberInfo(); */
        if (CommonUtil.isEmpty(userPayPassword)) {
            responseData.setStatus("0");
            responseData.setMsg("支付密码不能为空");
            return responseData;
        }
        if (CommonUtil.isEmpty(newPassword)) {
            responseData.setStatus("0");
            responseData.setMsg("新建密码不能为空");
            return responseData;
        }
        Boolean boolOld = userDataService.checkPayPassword(userId, userPayPassword);
        Boolean boolNew = userDataService.checkPayPassword(userId, newPassword);
        if (!boolOld) {
            responseData.setStatus("2");
            responseData.setData("输入的旧密码错误！请重新输入。");
            return responseData;
        }
        if (boolNew) {
            responseData.setStatus("3");
            responseData.setData("旧密码与新密码相同！请重新输入。");
            return responseData;
        }
        return userDataService.updateUserPayPasswordById(userId, newPassword);
    }

    /**
     * 查询支付密码and保存支付密码
     *
     * @param
     * @return
     */
    @GetMapping(value = "/saveUserPayPassword")
    public ResponseData selectUserPayPassword(String userId, String userPayPassword) {
        responseData.setData(null);
        if (CommonUtil.isEmpty(userId)) {
            responseData.setStatus("0");
            responseData.setMsg("用户Id不得能为空");
            return responseData;
        }
        if (CommonUtil.isEmpty(userPayPassword)) {
            responseData.setStatus("0");
            responseData.setMsg("密码不得能为空");
            return responseData;
        }
        return userDataService.saveUserPayPassword(userId, userPayPassword);
    }
    /**
     * 查询支付密码状态
     *
     * @param
     * @return
     */
    @GetMapping(value = "/selectUserPayPasswordState")
    @ResponseBody
    public ResponseObject selectUserPayPasswordState(String userId) {
        ResponseObject res = new ResponseObject();
        User seqId = userDataService.selectUser(userId);
        if (seqId==null) {
            res.setErroCode(0);
            res.setData("没有此用户");
        }else if (StringUtils.isBlank(seqId.getUserPayPassword())) {
            res.setErroCode(1);
            res.setData("用户没有密码状态");
        } else {
            System.out.println("用户有密码状态");
            res.setErroCode(2);
            res.setData(seqId.getUserPayPassword());
        }
        return res;
    }
    /**
     * 添加收货人地址
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/addCollectAddress")
    public ResponseObject addCollectAddress(CollectAddress id) {
        ResponseObject res = new ResponseObject();
        collectAddressService.insertCollectAddress(id);
        if (null == id) {
            res.setErroCode(1);
            res.setData("添加收货人地址失败");
        } else {
            res.setErroCode(0);
            res.setData("添加收货人地址成功");
        }
        return res;
    }
    /**
     * 修改收货人地址
     *
     * @param collectAddress
     * @return
     */
    @GetMapping(value = "/modifyCollectAddress")
    public ResponseObject modifyCollectAddress(CollectAddress collectAddress) {
        ResponseObject res = new ResponseObject();
        collectAddressService.updateCollectAddress(collectAddress);
        if (null == collectAddress) {
            res.setErroCode(1);
            res.setData("修改收货人地址失败");
        } else {
            res.setErroCode(0);
            res.setData("修改收货人地址成功");
            res.setData("修改收货人地址成功" + collectAddress);
            System.out.println("修改收货人地址成功" + collectAddress);
        }
        return res;
    }

    /**
     * 是否为默认收货地址"0"为默认"1"为非默认
     *
     * @param collectAddress
     * @return
     */
    @GetMapping(value = "/modifyWhetherAddress")
    public ResponseObject modifyWhetherAddress(CollectAddress collectAddress) {
        ResponseObject res = new ResponseObject();
        /*collectAddressService.modifyWhetherAddressAll(collectAddress);*/
        collectAddressService.modifyWhetherAddress(collectAddress);
        if (null == collectAddress) {
            res.setErroCode(1);
            res.setData("默认收货地址不能为空");
        } else {
            res.setErroCode(0);
            res.setData("此消息为默认地址");
            res.setData(collectAddress);
            System.out.println("此消息为默认地址" + collectAddress.toString());
        }
        return res;
    }

    /**
     * 删除收货人地址
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/deleteFoCollectAddress")
    public ResponseObject deleteFoCollectAddress(CollectAddress userId) {
        ResponseObject res = new ResponseObject();
        collectAddressService.deleteCollectAddress(userId);
        if (null == userId) {
            res.setErroCode(1);
            res.setData("删除失败");
        } else {
            res.setErroCode(0);
            res.setData("删除成功");
        }
        return res;
    }

    /**
     * 查询收货人地址
     *
     * @param userId
     * @return
     */
    @GetMapping(value = "/queryFoCollectAddress")
    @ResponseBody
    public Object queryFoCollectAddress(String userId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<CollectAddress> list = collectAddressService.list(userId);
            map.put("code", "1");
            map.put("msg", "查询成功并显示收货人信息");
            map.put("data", list);
            System.out.println("查询收货人信息" + map.toString());
        } catch (Exception ex) {
            logger.info(ex.getMessage());
            map.put("code", "0");
            map.put("msg", "查询失败");
            map.put("data", "");
        }
        return map;
    }

    /**
     * 只做登录验证，不做创建用户
     *
     * @param phone
     * @param code
     * @return
     */
    @GetMapping(value = "/userLoginNoSave")
    public Object loginWithOutSave(String phone, String code) {
        String url = userCenterUrl + ":808/loginNoVerifyCode";
        String param = "username=" + phone + "&password=" + code + "&appid=pcode";
        String ret = HttpRequestUtil.sendPost(url, param, false);
        JSONObject jsonObject = JSON.parseObject(ret);
        if (jsonObject.getString("code").equals("0")) {
            jsonObject.put("msg", "手机号或验证码不正确");
        }
        return jsonObject;
    }

    /**
     * 卡包用户登录
     */
    @PostMapping("login")
    public Object login(String username, String password) {
        String url = userCenterUrl + ":808/login";
        String param = "username=" + username + "&password=" + password;
        Object result = HttpRequestUtil.sendGet(url, param);
        return result;
    }

    /**
     * 通过手机号 设置空密码用户密码
     *
     * @param phone
     * @param code
     * @return
     */
    @PostMapping(value = "/setPasswordByPhone")
    public Object setPasswordByPhone(String phone, String code) {
        JSONObject re = remoteUserService.setSsoUserPasswordByPhone(phone, code);
        return re;
    }

    /**
     * 注册推荐码注册用户
     *
     * @param regCode
     * @param userId
     * @return
     */
    @PostMapping(value = "/userReg")
    public Object reg(String regCode, String userId) {
        String salt = ShiroKit.getRandomSalt(5);
        System.out.println("推荐注册用户：regCode=" + regCode + "  userId=" + userId);
        RemoteUser remoteUser = new RemoteUser();
        JSONObject json = remoteUserService.findSsoUserByUserId(userId);
        if (json.get("code").equals("1")) {
            remoteUser = (RemoteUser) json.get("user");
        }
        User user = new User();
        user.setUserId(remoteUser.getUserId());
        user.setAvatar(remoteUser.getImg());
        Integer sex = 2;
        String ssoSex = remoteUser.getSex();
        if (!StringUtils.isBlank(ssoSex)) {
            if (ssoSex.equals("0")) {
                sex = 0;
            }
            if (ssoSex.equals("1")) {
                sex = 1;
            }
        }
        user.setSex(sex);
        user.setAccount(remoteUser.getLoginAccount());
        Date date = DateUtils.parseDate(remoteUser.getBirthday());
        user.setBirthday(date);
        user.setCreatetime(new Date());
        user.setName(remoteUser.getUserName());
        user.setEmail(remoteUser.getEmail());
        user.setPhone(remoteUser.getMobilePhone());
        user.setRefer_reg_code(regCode);
        user.setReg_code(userDataService.getRegCode());
        user.setSalt(salt);
        userDataService.insert(user);
        return "ok";
    }

    @GetMapping(value = "/getPhoneByRegCode")
    public ResponseObject getPhoneByRegCode(String regCode) {
        if (StringUtils.isBlank(regCode)) {
            return ResponseUtil.ERROR("注册码不能为空");
        }
        User user = userDataService.findUserByRedCode(regCode);
        if (null == user) {
            return ResponseUtil.ERROR("注册码不存在");
        }
        String phone = user.getAccount();
        char[] cs = phone.toCharArray();
        cs[3] = '*';
        cs[4] = '*';
        cs[5] = '*';
        cs[6] = '*';
        phone = new String(cs);
        return ResponseUtil.SUCCESS(phone);
    }

    @GetMapping(value = "/regCodeUsedCount")
    public ResponseObject regCodeUsedCount(String regCode) {
        return packUserService.regCodeUsedCount(regCode);
    }

    @RequestMapping(value = "/recommendList")
    public ResponseObject recommendList(@RequestParam String userId) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        Wrapper<User> wrapperuser = new EntityWrapper<User>();
        wrapperuser.eq("user_id", userId);
        User parentUser = userDataService.selectOne(wrapperuser);
        if (parentUser == null) {
            res.setData("用户不存在");
            return res;
        }
        wrapperuser = new EntityWrapper<User>();
        wrapperuser.eq("refer_reg_code", parentUser.getReg_code());
        List<User> list = userDataService.selectList(wrapperuser);
        res.setErroCode(0);
        Map map = new HashMap<>();
        map.put("count", list.size());
        map.put("list", list);
        map.put("msg", "获取成功");
        res.setData(map);
        return res;
    }

    @RequestMapping(value = "/recommendByRegCode")
    public ResponseObject recommendByRegCode(@RequestParam String RegCode) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        Wrapper<User> wrapperuser = new EntityWrapper<User>();
        wrapperuser.eq("reg_code", RegCode);
        User parentUser = userDataService.selectOne(wrapperuser);
        if (parentUser == null) {
            res.setData("用户不存在");
            return res;
        }
        wrapperuser = new EntityWrapper<User>();
        wrapperuser.eq("refer_reg_code", parentUser.getReg_code());
        wrapperuser.setSqlSelect("user_id,name,birthday,sex,phone,createtime,reg_code,isauth");
        Page<User> page = new Page<>();
        page.setSize(100);
        page.setCurrent(0);
        page = userDataService.selectPage(page, wrapperuser);
        res.setErroCode(0);
        Map map = new HashMap<>();
        map.put("page", page);
        map.put("msg", "获取成功");
        res.setData(map);
        return res;
    }

    @RequestMapping(value = "/recommendPage")
    public ResponseObject recommendPage(@RequestParam String userId, @RequestParam int start, @RequestParam int pageSize) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        Wrapper<User> wrapperuser = new EntityWrapper<User>();
        wrapperuser.eq("user_id", userId);
        User parentUser = userDataService.selectOne(wrapperuser);
        if (parentUser == null) {
            res.setData("用户不存在");
            return res;
        }
//		wrapperuser = new EntityWrapper<User>();
//		wrapperuser.eq("refer_reg_code", parentUser.getReg_code());
//		wrapperuser.setSqlSelect("user_id,name,birthday,sex,phone,createtime,reg_code,isauth,avatar");
//		wrapperuser.and()
        Page<User> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(start);
//		page = userDataService.selectPage(page, wrapperuser);
        page = userDataService.recommendByRegCode(page, parentUser.getReg_code());
        res.setErroCode(0);
        Map map = new HashMap<>();
        map.put("page", page);
        map.put("msg", "获取成功");
        res.setData(map);
        return res;
    }

    /**
     * @param userId
     * @param pCode  奖励类型 注册 reg
     */
    @RequestMapping(value = "/getUserReward")
    public Object getUserReward(String userId, String pCode) {
        Map map = new HashMap();
        User user = userDataService.selectByUserId(userId);
        if (user == null) {
            map.put("code", "0");
            map.put("msg", "用户不存在");
            return map;
        }
        if (pCode.equals("reg")) {
            if (user.getIsauth()==null) {
                map.put("isAuth", 0);
            } else {
                map.put("isAuth", user.getIsauth());
            }
            Wrapper<ZoneUserAwardThree> warp = new EntityWrapper<>();
            warp.eq("userId", userId);
            List<ZoneUserAwardThree> list = zoneUserAwardThreeService.selectList(warp);
            Integer award = 0;
            Integer isNew = 0;//0 旧，1 新
            if (list.size() > 0) {
                award = list.get(0).getUserIdAward();
                isNew = 1;
            } else {
                Wrapper<ZoneUserAward> warp2 = new EntityWrapper<>();
                warp2.eq("userId", userId);
                List<ZoneUserAward> list2 = zoneUserAwardService.selectList(warp2);
                if (list2.size() > 0) {
                    award = list2.get(0).getUserIdAward();
                    isNew = 1;
                }
            }
            map.put("award", award);
            map.put("isNew", isNew);
        }
        return map;
    }
}